//
//  MainViewController.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/4/7.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import UIKit
import TabPageViewController
class MainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true//隐藏导航
    }
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        switch item.tag{
        case 0:
           pageTag = "0"
        case 1:
            pageTag = "1"
        case 2:
             pageTag = "2"
        case 3:
            pageTag = "3"
        default:
            pageTag = "0"
        }
    }
}
extension MainViewController {
    
    fileprivate func setUpUI(){
        //主页
        addChildViewController(title:"主页", vc: HomeViewController(), imageName: "home_tab_home",tag:0)
        //订单管理
        var option = TabPageOption()
        option.tabHeight = CGFloat(40)
        option.currentColor = appMainColor
        let tc = TabPageViewController.create()
        
        let caraudit = OtherOrderController()
        caraudit.interfaceUrl = "caraudit"
        
        let driveaudit = OtherOrderController()
        driveaudit.interfaceUrl = "drivelicenseaudit"
        
        let driving = OtherOrderController()
        driving.interfaceUrl = "drivinglicense"
        
        let drivelicense = OtherOrderController()
        drivelicense.interfaceUrl = "drivelicense"
        
        let carinsurance = OtherOrderController()
        carinsurance.interfaceUrl = "carinsurance"
        
        let carfine = OtherOrderController()
        carfine.interfaceUrl = "carfine"
        
        let carmortgage = OtherOrderController()
        carmortgage.interfaceUrl = "carmortgage"
        
        let carpurchase = OtherOrderController()
        carpurchase.interfaceUrl = "carpurchase"
        
        let cartrade = OtherOrderController()
        cartrade.interfaceUrl = "cartrade"
        
        let tabItems:[(viewController: UIViewController, title: String)] = [(ViolationViewController(), "违章"), (caraudit, "审车"), (driveaudit, "驾证审核"), (driving, "行驶证补办"), (drivelicense, "驾证补换"), (carinsurance, "保险"), (carfine, "罚款单"), (carmortgage, "抵押贷款"), (carpurchase, "新车代购"), (cartrade, "二手车买卖")]
        tc.tabItems = tabItems
        tc.option = option
        addChildViewController(title:"订单查询", vc: tc, imageName: "home_tab_my_order",tag:1)
        if("2" == isEmployee){
            let caraudit2 = OtherOrderController()
            caraudit2.interfaceUrl = "caraudit"
            
            let driveaudit2 = OtherOrderController()
            driveaudit2.interfaceUrl = "drivelicenseaudit"
            
            let driving2 = OtherOrderController()
            driving2.interfaceUrl = "drivinglicense"
            
            let drivelicense2 = OtherOrderController()
            drivelicense2.interfaceUrl = "drivelicense"
            
            let carinsurance2 = OtherOrderController()
            carinsurance2.interfaceUrl = "carinsurance"
            
            let carfine2 = OtherOrderController()
            carfine2.interfaceUrl = "carfine"
            
            let carmortgage2 = OtherOrderController()
            carmortgage2.interfaceUrl = "carmortgage"
            
            let carpurchase2 = OtherOrderController()
            carpurchase2.interfaceUrl = "carpurchase"
            
            let cartrade2 = OtherOrderController()
            cartrade2.interfaceUrl = "cartrade"
            
            let tabItems:[(viewController: UIViewController, title: String)] = [(ViolationViewController(), "违章"), (caraudit2, "审车"), (driveaudit2, "驾证审核"), (driving2, "行驶证补办"), (drivelicense2, "驾证补换"), (carinsurance2, "保险"), (carfine2, "罚款单"), (carmortgage2, "抵押贷款"), (carpurchase2, "新车代购"), (cartrade2, "二手车买卖")]
            
            let mine_tc = TabPageViewController.create()
            mine_tc.tabItems = tabItems
            mine_tc.option = option
            addChildViewController(title:"订单管理", vc: mine_tc, imageName: "home_tab_saunter",tag:2)
        }
        addChildViewController(title:"个人中心", vc: PersonalViewController(), imageName: "home_tab_personal",tag:3)
    }
    
    fileprivate func addChildViewController(title:String,vc:UIViewController,imageName:String,tag:Int){
        
        vc.title = title
        //设置默认图片
        vc.tabBarItem.image = UIImage(named:imageName)
        //设置高亮图片
        vc.tabBarItem.selectedImage = UIImage(named: imageName +  "_selected")?.withRenderingMode(.alwaysOriginal)
        //设置高亮字体颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:appMainColor],for: .selected)
        //设置默认字体颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.gray],for: .normal)
        //设置字体位置
        vc.tabBarItem.titlePositionAdjustment = UIOffsetMake(1,-4)
        //设置图片位置
        vc.tabBarItem.imageInsets=UIEdgeInsetsMake(1, 0, -1, 0);
        
        vc.tabBarItem.tag = tag
        
        let nav = UINavigationController(rootViewController: vc)

        //添加子控制器
        addChildViewController(nav)
    }
}
